from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session, relationship

"""
scoped_session本质上也是一个方法，它首先通过session工厂创建session对象，然后对session对象进行相应的管理。
scoped session采用的是注册模式。
scoped session算是一个全局变量。
"""

from sqlalchemy import Column, Integer, VARCHAR, TEXT, DOUBLE, DATE
from zope.sqlalchemy import register

DBSession = scoped_session(sessionmaker(), scopefunc=lambda: 'default')
register(DBSession)
Base = declarative_base()


class Student(Base):
    __tablename__ = 'student'
    id = Column(Integer, primary_key=True, nullable=False)
    name = Column(VARCHAR(255), unique=True)
    percent = Column(Integer)


class Ftprecode(Base):
    __tablename__ = 'ftprecode'
    id = Column(Integer, primary_key=True, nullable=False, comment="主键")
    department = Column(VARCHAR(255), nullable=False, unique=False, comment="部门")
    employee = Column(VARCHAR(255), nullable=True, unique=False, comment="员工")
    account = Column(VARCHAR(255), nullable=False, unique=True, comment="账号")
    pwd = Column(VARCHAR(255), nullable=False, unique=False, comment="密码")
    groupname = Column(VARCHAR(255), nullable=False, unique=False, comment="组别")


class TaxMonthIncomeTable(Base):
    __tablename__ = 'tax_month_income_table'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True, comment="主键")
    classes = Column(VARCHAR(50), nullable=False, comment="人员分类")
    addr = Column(VARCHAR(255), nullable=False, comment="属地")
    id_card = Column(VARCHAR(50), nullable=False, comment="身份证")
    name = Column(VARCHAR(50), nullable=False, comment="姓名")
    calendar_month = Column(DATE, nullable=False, comment="自然月")
    filename = Column(VARCHAR(255), nullable=False, comment="文件名")
    is_first = Column(Integer, nullable=False, comment="是否首次插入")
    month_income = Column(DOUBLE, nullable=False, default=0, comment="月收入")
    month_taxincome = Column(DOUBLE, nullable=False, default=0, comment="月应税")
    month_tax = Column(DOUBLE, nullable=False, default=0, comment="月个税")
    entry_date = Column(DATE, nullable=False, comment="入职日期")
    dtime = Column(DOUBLE, nullable=False, default=0, comment="最后更新时间")
    operations_personnel = Column(VARCHAR(50), nullable=False, comment="操作运营")
    isgrant = Column(Integer, nullable=False, default=0, comment="是否发放")


class TaxIncomeTable(Base):
    __tablename__ = 'tax_income_table'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
    addr = Column(VARCHAR(255), nullable=False, comment="属地")
    id_card = Column(VARCHAR(50), nullable=False, comment="身份证")
    name = Column(VARCHAR(50), nullable=False, comment="姓名")
    calendar_month = Column(DATE, nullable=False, comment="自然月")
    cumulative_income = Column(DOUBLE, nullable=False, default=0, comment="累计收入")
    cumulative_taxincome = Column(DOUBLE, nullable=False, default=0, comment="累计应税")
    cumulative_tax = Column(DOUBLE, nullable=False, default=0, comment="累计个税")
    entry_date = Column(DATE, nullable=False, comment="入职日期")
    dtime = Column(DOUBLE, nullable=False, default=0, comment="最后更新时间")
    create_time = Column(DOUBLE, nullable=False, default=0, comment="创建时间")
    cidsum = Column(TEXT, nullable=False, comment="底表子id子集")
    version = Column(Integer, nullable=False, comment="版本号")
